<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        inline size="mini"
        :model="where"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-form-item style="width: 350px">
          <el-date-picker
              v-model="wherePicker"
              type="daterange"
              align="right"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              size="mini"
              :picker-options="pickerOptions">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
              type="primary"
              icon="el-icon-search"
              class="ele-btn-icon"
              @click="reload">查询
          </el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            size="small"
            type="primary"
            class="ele-btn-icon"
            icon="el-icon-download"
            @click="exportData">导出
          </el-button>
        </template>
        <!-- 日志类型列 -->
        <template slot="content" slot-scope="{row}">
          <div style="cursor: pointer;">
            <el-tag v-if="row.content" type="success" size="mini" @click="showParam(row.param)">{{row.content}}</el-tag>
            <el-tag v-else type="info" size="mini" @click="showParam(row.param)">原始请求</el-tag>
          </div>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-view"
            @click="openDetail(row)"
            v-if="permission.includes('sys:operlog:detail')">详情
          </el-link>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 详情弹窗 -->
    <oper-log-detail :visible.sync="showInfo" :data="current||{}"/>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import OperLogDetail from './operlog-detail';
import {download} from "@/utils/download";

export default {
  name: 'SystemOperLog',
  components: {OperLogDetail},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/actionlog/index',
      // 表格列配置
      columns: [
        {
          prop: 'app',
          label: '系统',
          align: 'center',
          showOverflowTooltip: true,
          width: 100
        },
        {
          prop: 'username',
          label: '账号',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100
        },
        {
          prop: 'module',
          label: '模块',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100
        },
        {
          prop: 'title',
          label: '操作',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 150
        },
        {
          prop: 'content',
          label: '内容',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100,
          slot: 'content'
        },
        {
          prop: 'url',
          label: '请求地址',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100
        },
        {
          prop: 'ip',
          label: 'IP地址',
          align: 'center',
          showOverflowTooltip: true,
          width: 130
        },
        {
          prop: 'create_time',
          label: '时间',
          align: 'center',
          showOverflowTooltip: true,
          width: 160,
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
      
      ],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      // 表格搜索条件
      wherePicker: null,
      where: {},
      // 当前选中数据
      current: null,
      // 是否显示查看弹窗
      showInfo: false,
      routerLoading: false,
      routerMap: []
    };
  },
  watch: {
    wherePicker(val) {
      this.where.date = val
      console.log('xxx', val)
    }
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.wherePicker = null
      this.where = {}
      this.reload()
    },
    /* 日期选择改变回调 */
    onDateRangeChoose() {
      if (this.daterange && this.daterange.length === 2) {
        this.where.createTimeStart = this.daterange[0];
        this.where.createTimeEnd = this.daterange[1];
      } else {
        this.where.createTimeStart = null;
        this.where.createTimeEnd = null;
      }
    },
    /* 详情 */
    openDetail(row) {
      this.current = row;
      this.showInfo = true;
    },
    showParam(param) {
      this.$alert(param, '原始请求')
    },
    /* 导出数据 */
    exportData() {
      download('/actionlog/export', this.where)
    }
  }
}
</script>

<style scoped>
</style>
